<template>
  <admin-layout class="ArticleShow">
    <h1>ArticleCreate</h1>
    <el-form :model="articleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="标题" prop="title">
        <el-input v-model="articleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input type="textarea" v-model="articleForm.desc"></el-input>
      </el-form-item>
      <!--
        vue 富文本：
        https://search.gitee.com/?skin=rec&type=repository&q=vue%20%E5%AF%8C%E6%96%87%E6%9C%AC
        vue-quill-editor：
        https://gitee.com/jeffka/vue-quill-editor
      -->
      <el-form-item label="发布者" prop="author">
        <el-select v-model="articleForm.author" placeholder="请选择发布者">
          <el-option label="人民日报" value="人民日报"></el-option>
          <el-option label="美国日报" value="美国日报"></el-option>
          <el-option label="一盏日报" value="一盏日报"></el-option>
          <el-option label="超厉害日报" value="超厉害日报"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发布时间" required>
        <el-form-item prop="created_at">
          <el-date-picker type="date" placeholder="选择发布日期" v-model="articleForm.created_at"></el-date-picker>
        </el-form-item>
      </el-form-item>
      <div class="detail">
        <label>
          详情
        </label>
        <QuillEditor contentType="html" v-model:content="articleForm.detail" class="editor" theme="snow" />
      </div>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </admin-layout>
</template>

<script>
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css';


export default {
  name: "ArticleCreate",
  components: {
    QuillEditor
  },
  data() {
    return {
      articleForm: {
        title: '',
        desc: '',
        author: '',
        created_at: '',
        detail: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 5, max: 20, message: '长度在 5 到 20 个字符', trigger: 'change' }
        ],
        desc: [
          { required: true, message: '请填写文章描述', trigger: 'blur' }
        ],
        author: [
          { required: true, message: '请选择发布者', trigger: 'change' }
        ],
        created_at: [
          { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
        ],
        detail: [
          { required: true, message: '请填写文章详情', trigger: 'blur' }
        ]
      }
    }
  },
  created() {

  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // alert('submit!');
          // console.log(this.articleForm)
          try {
            await this.$store.dispatch('article/createArticle', this.articleForm)
            this.$message.success('提交成功')
            this.$router.push({
              name: 'Article'
            })
          } catch (e) {
            this.$message.success('网络错误')
          }
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style lang="less">
.detail {
  //display: flex;
  width: 100%;

  > label {
    width: 100px;
  }

  .editor {
    height: 300px;
  }
}
</style>
